package com.dormitory_springboot.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dormitory_springboot.bean.Homemaster;
import com.dormitory_springboot.form.HomemasterForm;
import com.dormitory_springboot.form.RuleForm;
import com.dormitory_springboot.service.HomemasterService;
import com.dormitory_springboot.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @auther 夏杨
 **/
@RestController
@RequestMapping("/homemaster")
public class HomemasterController {
    @Autowired
    HomemasterService homemasterService;
    @GetMapping("/login")
    public ResultVO login(RuleForm ruleForm){
        return homemasterService.login(ruleForm);
    }
    @GetMapping("/addHomemaster")
    public void addHomemaster(HomemasterForm homemasterForm){
        Homemaster homemaster = new Homemaster(
                homemasterForm.getHmid(),
                "a123456!",
                homemasterForm.getName(),
                homemasterForm.getSex(),
                homemasterForm.getPhone(),
                homemasterForm.getEmail()
        );
        homemasterService.save(homemaster);
    }
    @GetMapping("/getHomemasters/{page}/{pagesize}")
    public Page getHomemasters(@PathVariable("page") int page, @PathVariable("pagesize") int pagesize,@RequestParam("hmid")String hmid){
        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.like("hmid",hmid);
        Page<Homemaster> p = new Page<>(page,pagesize);
        return homemasterService.page(p,queryWrapper);
    }
    @GetMapping("/deleteOneHM/{hmid}")
    public void deleteOneHM(@PathVariable("hmid") String hmid){
        homemasterService.removeById(hmid);
    }
    @GetMapping("/getHMById/{hmid}")
    public Homemaster getHMById(@PathVariable("hmid")String hmid){
        return homemasterService.getById(hmid);
    }
    @GetMapping("/edithomemaster/{hmid}")
    public void edithomemaster(@PathVariable("hmid") String hmid,Homemaster homemaster){
        UpdateWrapper<Homemaster> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("hmid",hmid);
        homemasterService.update(homemaster,updateWrapper);
    }
    @GetMapping("/changePassById/{password}/{hmid}")
    public void changePassById(@PathVariable("password")String password,@PathVariable("hmid")String hmid){
        UpdateWrapper<Homemaster> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("hmid",hmid).set("password",password);
        homemasterService.update(updateWrapper);
    }
    @GetMapping("/countAllByYear")
    public int countAll(String year){
        QueryWrapper qw = new QueryWrapper();
        qw.likeRight("hmid",year);
        return homemasterService.count(qw);
    }
}
